package com.headwind.logistics.permission.controller;

import com.headwind.logistics.common.util.LayuiUtil;
import com.headwind.logistics.common.util.RestResult;
import com.headwind.logistics.common.util.ResultCode;
import com.headwind.logistics.common.util.ResultGenerator;
import com.headwind.logistics.common.pojo.Menu;
import com.headwind.logistics.permission.pojo.MenuTree;
import com.headwind.logistics.common.pojo.Role;
import com.headwind.logistics.permission.service.intl.RoleServiceIntl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 小恶魔
 * @Date 2020-07-14 21:42
 * @Version 1.0
 * @DESCRIPTION 角色控制层
 */
@Controller
public class RoleController {
    //业务逻辑层
    @Resource
    private RoleServiceIntl roleServiceIntl;

    //接口信息生成工具
    @Resource
    private ResultGenerator resultGenerator;

    /**
     * 查询角色  分页
     * @return
     */
    @GetMapping("/admin/queryRolePage")
    @ResponseBody
    public LayuiUtil queryRolePage(Integer page,Integer limit){
        //获取角色集合
        List<Role> roles = roleServiceIntl.queryRoleMenu(page, limit);

        return LayuiUtil.data(roleServiceIntl.queryRoleMenu(null, null).size(),roles);
    }

    /**
     * 根据角色id获取菜单
     * @param roleId
     * @return
     */
    @PostMapping("/admin/queryMenuByRoleId/{roleId}")
    @ResponseBody
    public RestResult queryMenuByRoleId(@PathVariable("roleId") Long roleId){

        try{

            List<Menu> menuList = roleServiceIntl.queryMenuByRoleId(roleId);

            return resultGenerator.getSuccessResult(menuList);
        }catch (Exception e){
            //异常返回信息
            return resultGenerator.getFreeResult(ResultCode.INTERNAL_SERVER_ERROR,"服务器错误",null);
        }



    }

    /**
     * 根据菜单id获取角色
     * @param menuId
     * @return
     */
    @PostMapping("/admin/queryRoleByMenuId/{menuId}")
    @ResponseBody
    public RestResult queryRoleByMenuId(@PathVariable("menuId")Long menuId){

        try{
            List<Role> roles = roleServiceIntl.queryRoleByMenuId(menuId);

            return resultGenerator.getSuccessResult(roles);

        }catch(Exception e){
            //异常返回信息
            return resultGenerator.getFreeResult(ResultCode.INTERNAL_SERVER_ERROR,"服务器错误",null);
        }
    }

    /**
     * 获取全部角色信息
     * @return
     */
    @PostMapping("/admin/queryAllRo/{menuId}")
    @ResponseBody
    public RestResult queryAllRole(@PathVariable("menuId")Long menuId){
        Map<String,Object> map = new HashMap<>();
        try{
            List<Role> roles = roleServiceIntl.queryRoleMenu(null,null);
            //根据菜单id获取角色信息
            List<Role> roleList = roleServiceIntl.queryRoleByMenuId(menuId);
            map.put("allRole",roles);
            map.put("roleByMenuId",roleList);

            return resultGenerator.getSuccessResult(map);

        }catch(Exception e){
            //异常返回信息
            return resultGenerator.getFreeResult(ResultCode.INTERNAL_SERVER_ERROR,"服务器错误",null);
        }
    }

    /**
     * 查询树形菜单
     * @return
     */
    @PostMapping("/admin/queryMenuTree/{roleId}")
    @ResponseBody
    public RestResult queryMenuTree(@PathVariable("roleId")Long roleId){

        try{

            List<MenuTree> menuTrees = roleServiceIntl.queryAllRecursion(roleId);

            return resultGenerator.getSuccessResult(menuTrees);

        }catch(Exception e){
            return resultGenerator.getFailResult("错误");
        }


    }

    /**
     * 添加角色菜单关联表
     * @param roleId
     * @param menuId
     * @return
     */
    @PostMapping("/admin/addRoleMenu/{roleId}")
    @ResponseBody
    public RestResult addMenuRole(@PathVariable("roleId")Long roleId,String menuId){

        try{
            int count = roleServiceIntl.addMenuRole(roleId, menuId);

            return resultGenerator.getSuccessResult("已修改了"+count+"条数据成功",null);

        }catch(Exception e){
            return resultGenerator.getFailResult("错误");
        }
    }

    /**
     * 删除角色菜单关联表
     * @param roleId
     * @param menuId
     * @param menuManagerId
     * @return
     */
    @PostMapping("/admin/deleteRoleMenu/{roleId}")
    @ResponseBody
    public RestResult deleteMenuRole(@PathVariable("roleId")Long roleId,Long menuId,Long menuManagerId){

        try{
            int count = roleServiceIntl.deleteMenuRole(roleId, menuId, menuManagerId);


            return resultGenerator.getSuccessResult("已修改"+count+"条数据！",null);


        }catch(Exception e){
            return resultGenerator.getFailResult("错误");
        }
    }

}
